#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "zigbee.h"
#include "ddebug.h"
#include "recvmsg_type.h"
#include "shmctl.h"
#include "global_data.h"
#include "gprs.h"
#include "sql_ctl.h"

extern int sto_auto_status;
void  *zigbee_recv(void *argc)
{
	int fd_zigbee = 4;
	char *gprs_buf = NULL;
	char *phone_number = NULL;
	int x;		//a temp var
	send_msg_t recv_buf = {0};
	if(fd_zigbee < 0)
	{
		DDEBUG(P_URGENCY,"<<ZIGBEE>> serial init a9");
		return NULL;
	}
	struct shm_addr *shm_buf = NULL;//(struct shm_addr *)malloc(sizeof(struct shm_addr));

	DDEBUG(P_NORMAL,"<<ZIGBEE>> in the recv!\n");
	DDEBUG(P_NORMAL,"<<ZIGBEE>> Size %d\n",sizeof (send_msg_t ));
	DDEBUG(P_NORMAL,"<<ZIGBEE>> zigbee_recv is work \n");
	while(1)
	{
		memset(&recv_buf,0,sizeof(send_msg_t));
		if( 0 >read_zigbee_a9(fd_zigbee, (char *)&recv_buf, sizeof(send_msg_t)))
		{
			DDEBUG(P_URGENCY,"<<ZIGBEE>> bad read!\n");
			break;
		}
		DDEBUG(P_NORMAL,"<<ZIGBEE>> recved: ");
		for(x = 0; x < 28; x++)
			DDEBUG(P_NORMAL,"%02x",((char *)(&recv_buf))[x]);
		DDEBUG(P_NORMAL,"\n");
		DDEBUG(P_NORMAL,"<<ZIGBEE>> recv: machine_num %d\n",recv_buf.machine_num);
		DDEBUG(P_NORMAL,"<<ZIGBEE>> recv: msg_type %d\n",recv_buf.msg_type);
	
		if(recv_buf.machine_num > 7)
		{
			DDEBUG(P_NORMAL,"<<ZIGBEE>> recv a wrong sto: %d,will ignore it -_-\n",recv_buf.machine_num);
			continue;
		}

		sto_auto_status &= ~(0xf <<((recv_buf.machine_num) * 4));
		switch(recv_buf.msg_type)
		{
			case 1:
				DDEBUG(P_NORMAL,"<<ZIGBEE>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ env\n");
				DDEBUG(P_NORMAL,"<<ZIGBEE>> temp:%d.%d  ",recv_buf.data.env.tem_u.htem,recv_buf.data.env.tem_u.ltem);
				DDEBUG(P_NORMAL,"hum :%d.%d  ",recv_buf.data.env.hum_u.hhum,recv_buf.data.env.hum_u.lhum);
				DDEBUG(P_NORMAL,"light:%d\n",recv_buf.data.env.light_u.light);
				DDEBUG(P_NORMAL,"<<ZIGBEE>> acc x:%d y:%d z:%d\n",recv_buf.data.env.acc_u.x,recv_buf.data.env.acc_u.y,recv_buf.data.env.acc_u.z);
				DDEBUG(P_NORMAL,"<<ZIGBEE>> acd :%d  ",recv_buf.data.env.adc_u.adc1);
				DDEBUG(P_NORMAL,"acc :%d\n",recv_buf.data.env.adc_u.adc2);
				DDEBUG(P_NORMAL,"<<ZIGBEE>> sta: led: %d fan: %d ",recv_buf.data.env.status_u.led_stat,recv_buf.data.env.status_u.fan_stat);
				DDEBUG(P_NORMAL,"buzz: %d seg: %d\n",recv_buf.data.env.status_u.buzz_stat,recv_buf.data.env.status_u.seg7_stat);

				shmget_env(&shm_buf);
				DDEBUG(P_NORMAL,"<<ZIGBEE>> -----------------> ----------------------> sto:%d  status:%d\n",recv_buf.machine_num,
						shm_buf->rt_status.storage_no[recv_buf.machine_num].storage_status);
				if(shm_buf->rt_status.storage_no[recv_buf.machine_num].storage_status == 0)
				{
					send_msg_t mmsg = {0};
					mmsg.machine_num = recv_buf.machine_num;
					mmsg.msg_type = 5;
					sql_read_limit(recv_buf.machine_num, &(mmsg.data.limit));
					write_zigbee_a9(4, (char *)&mmsg, sizeof(send_msg_t));
				
					shm_buf->rt_status.storage_no[recv_buf.machine_num].temperatureMAX = mmsg.data.limit.tem_max.htem;
					shm_buf->rt_status.storage_no[recv_buf.machine_num].temperatureMIN = mmsg.data.limit.tem_min.htem;
					shm_buf->rt_status.storage_no[recv_buf.machine_num].humidityMAX = mmsg.data.limit.hum_max.hhum;
					shm_buf->rt_status.storage_no[recv_buf.machine_num].humidityMIN = mmsg.data.limit.hum_min.hhum;
					shm_buf->rt_status.storage_no[recv_buf.machine_num].illuminationMAX = mmsg.data.limit.light_max.light;
					shm_buf->rt_status.storage_no[recv_buf.machine_num].illuminationMIN = mmsg.data.limit.light_min.light;
				}
				shm_buf->rt_status.storage_no[recv_buf.machine_num].storage_status = 1;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].temperature = recv_buf.data.env.tem_u.htem  + (recv_buf.data.env.tem_u.ltem % 10) * 0.1;

				shm_buf->rt_status.storage_no[recv_buf.machine_num].humidity = recv_buf.data.env.hum_u.hhum  + (recv_buf.data.env.hum_u.lhum % 10) * 0.1;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].illumination = recv_buf.data.env.light_u.light;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].battery = recv_buf.data.env.adc_u.adc2 * 0.1;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].adc = recv_buf.data.env.adc_u.adc1 * 0.1;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].x = recv_buf.data.env.acc_u.x;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].y = recv_buf.data.env.acc_u.y;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].z = recv_buf.data.env.acc_u.z;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].led_status = (recv_buf.data.env.status_u.led_stat == 1)?1:0;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].fan_status = (recv_buf.data.env.status_u.fan_stat == 1)?1:0;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].buzzer_status = (recv_buf.data.env.status_u.buzz_stat ==1)?1:0;
				shm_buf->rt_status.storage_no[recv_buf.machine_num].seg_status = (recv_buf.data.env.status_u.seg7_stat == 0xff)?0xf:recv_buf.data.env.status_u.seg7_stat;
				
				shmset_env(shm_buf);

				break;
			case 2:
				DDEBUG(P_NORMAL,"<<ZIGBEE>> ##################################### rfid\n");
				DDEBUG(P_NORMAL,"<<ZIGBEE>> recv rfid: %d\n",recv_buf.data.rfid.id);
				for(x=0; x < 24; x++)
				{
					DDEBUG(P_NORMAL,"%2x",recv_buf.data.rfid.datablock[x]);
				}
				
				DDEBUG(P_NORMAL,"\n");
#ifndef GOODCHARSIMPLE
				sql_ctl_goods(recv_buf.machine_num,recv_buf.data.rfid.id,recv_buf.data.rfid.datablock,recv_buf.data.rfid.eeprom[0]);
#else
		//		sql_ctl_goods(recv_buf.data.rfid.id,recv_buf.data.rfid.eeprom[0]);
#endif
				;
				break;
			case 3:	
				
				DDEBUG(P_URGENCY,"<<ZIGBEE>> something wrong with this M0.this is a command message....??????????????????????????\n");
						;
				break;
			case 4:
				DDEBUG(P_NORMAL,"<<ZIGBEE>> +++++++++++++++++++++++++++++++++++++++++ warning\n");
				gprs_buf = (char *)malloc(100);
				bzero(gprs_buf, 100);
				shmget_env(&shm_buf);
				int mask = shm_buf->rt_status.storage_no[recv_buf.machine_num].warningMask;
				DDEBUG(P_NORMAL,"<<ZIGBEE>> mask:^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->%x\n",mask);
				switch(recv_buf.data.warning.id)
				{
					case 1:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> Storage %d tem is warning......\n",recv_buf.machine_num);
						if(recv_buf.data.warning.status == 1 && (!((mask >>20)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d temperature is too high!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<20;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s   ---send\n",gprs_buf);
						}
						else if(recv_buf.data.warning.status == 2&& (!((mask >>18)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d temperature is too low!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<18;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						break;
					case 2:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> Storage %d hum is warning........ \n",recv_buf.machine_num);
						if(recv_buf.data.warning.status == 1 && (!((mask >>16)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d humidity is too high!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<16;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						else if(recv_buf.data.warning.status == 2&& (!((mask >>14)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d humidity is too low!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<14;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						break;

					case 3:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> Storage %d light is %d..............\n",recv_buf.machine_num,recv_buf.data.warning.status);
						if(recv_buf.data.warning.status == 1&& (!((mask >>12)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d light is too high!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<12;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						else if(recv_buf.data.warning.status == 2&& (!((mask >>10)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d light is too low!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<10;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}

						break;
					case 4:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> Storage %d adc is warning.......\n",recv_buf.machine_num);
						if(recv_buf.data.warning.status == 1&& (!((mask >>8)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d adc is too high!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<8;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						else if(recv_buf.data.warning.status == 2&& (!((mask >>6)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d adc is too low!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<6;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}

						break;
					case 5:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> Storage %d acc is warning.........\n",recv_buf.machine_num);
						if(recv_buf.data.warning.status == 1&& (!((mask >>4)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d acc is too high!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<4;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}
						else if(recv_buf.data.warning.status == 2&& (!((mask >>2)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d acc is too low!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<2;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}

						break;
					case 6:
						DDEBUG(P_URGENCY,"<<ZIGBEE>> mask:%d,Storage %d has collapsed.......... \n",!((mask>>0)&0x3),recv_buf.machine_num);
						if((!((mask >>0)&0x3)))
						{
							sprintf(gprs_buf,"<<ZIGBEE>> Storage %d has collapsed!\n",recv_buf.machine_num);
							send_en_message(gprs_buf,phone_number);
							mask |= 0x3<<0;
							DDEBUG(P_URGENCY,"<<ZIGBEE>> send %s .......send\n",gprs_buf);
						}

					default:
						;
				}

				free(gprs_buf);
				
				shm_buf->rt_status.storage_no[recv_buf.machine_num].warningMask = mask;

				shmset_env(shm_buf);
				break;
			case 5:
				DDEBUG(P_NORMAL,"<<ZIGBEE>> @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  limit\n");
				send_msg_t mmsg = {0};
				mmsg.machine_num = recv_buf.machine_num;
				mmsg.msg_type = 5;
				sql_read_limit(recv_buf.machine_num, &(mmsg.data.limit));
				write_zigbee_a9(4, (char *)&mmsg, sizeof(send_msg_t));
				break;
			default:
				DDEBUG(P_URGENCY,"<<ZIGBEE>> read_zigbee_bad msg^^^^^^^^^^^^^^^^^ - - ^^^^^^^^^^^^^^\n");
			
		}

	}
	return 0;
}
